package com.tsy.graduate.utils;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.tsy.graduate.httpdto.ObjectCollectionResponse;
import com.tsy.graduate.httpdto.ObjectDataResponse;
import com.tsy.graduate.exception.BusinessException;
import com.tsy.graduate.exception.ExceptionEnum;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class GsonUtils {
    private static final GsonBuilder gsonBuilder = new GsonBuilder();

    public static Gson gson() {
        return gsonBuilder.serializeNulls().create();
    }

    public static JsonObject toJsonObject(String json) {
        return new JsonParser().parse(json).getAsJsonObject();
    }

    public static JsonArray toJsonArray(String json) {
        return new JsonParser().parse(json).getAsJsonArray();
    }

    public static Object toObject(JsonObject jsonObject) {
        return gson().fromJson(jsonObject, Object.class);
    }

    public static Object toObject(JsonArray jsonArray) {
        return gson().fromJson(jsonArray, Object.class);
    }

    public static Map<String, Object> toMap(String str) {
        Map<String, Object> gsonMap = gson().fromJson(str, new TypeToken<Map<String, Object>>() {
        }.getType());
        return gsonMap;
    }

    /**
     * bean  ->  json
     *
     * @param object
     * @return
     * @author daozhang
     */
    public static String encode(Object object) {
        return gson().toJson(object);
    }

    /**
     * json  ->  bean
     *
     * @param json
     * @param clz
     * @return
     * @author daozhang
     */
    public static <T> T decode(String json, Class<T> clz) {
        return gson().fromJson(json, clz);
    }


    public static <T> List<T> decodeList(String json, Class<T> clz) {
        return fromJsonList(json, clz);
    }

    public static <T> List<T> fromJsonList(String json, Class<T> clazz) {
        List<T> lst = new LinkedList<>();
        try {

            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (final JsonElement elem : array) {
                lst.add(new Gson().fromJson(elem, clazz));
            }
        } catch (Exception e) {
            System.out.println("出错json str = "+json);
            throw new BusinessException(2000,"Gson解析出错");
        }
        return lst;
    }

    /**
     * 解析ObjectDataResponse
     *
     * @return
     */
    public static <T> ObjectDataResponse<T> decodeDataRes(Class<T> clz, String json) {
        Gson gson = gson();
        return gson.fromJson(json, new TypeToken<ObjectDataResponse<T>>() {
        }.getType());
    }

    @SuppressWarnings("all")
    public static <T> ObjectCollectionResponse<T> decodeCollectionRes(Class<T> clz, String json) {
        ObjectCollectionResponse response = decode(json, ObjectCollectionResponse.class);
        Map<String, Object> map = toMap(json);
        String data = encode(map.get("data"));
        Collection<T> collection = gson().fromJson(data, new TypeToken<Collection<T>>() {
        }.getType());
        response.setData(collection);
        return response;
    }
}
